<?php

/**
 * Mallkit Featured Products Module
 *
 * @version 	$Id: mod_mallkit_featured.php 21 2009-01-30 10:04:54Z thomas@mallkit.org $
 * @package 	Mallkit eCommerce
 * @subpackage 	Modules
 *
 * @copyright	Copyright (C) 2004-2009 saurabh - All rights reserved.
 * @license 	GNU/GPL, see LICENSE.php
 *
 * This file is part of Mallkit eCommerce.
 *
 * Mallkit eCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Mallkit eCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @website	http://www.mallkit.org
 */
	
// no direct access
defined('_JEXEC') or die('Restricted access');
jimport('phpxmlrpc.xmlrpc');

// get joomla instance of client
$host = JURI::getInstance();
	
// get host of clients joomla instance and set it to the $host variable
$host = $host->getHost();
	
// get the clients true joomla instance root (path-only)
$instance_path = JURI::root(true);
// append the xmlrpc directory and set to the $path variable
$xml_path = JURI::root(true)."/xmlrpc/";
		
// create an instance of client implementation
$client = new xmlrpc_client($xml_path, $host);
	
// define global xmlrpc variable types
global $xmlrpcI4, $xmlrpcInt, $xmlrpcBoolean, $xmlrpcDouble, $xmlrpcString, $xmlrpcDateTime, $xmlrpcBase64, $xmlrpcArray, $xmlrpcStruct, $xmlrpcValue;

$numtodisplay = $params->get('numtodisplay', 5);  // maximum number of items to display
$categories = $params->get('categories', null);  // filter products from categories
$display_layout = $params->get( 'display_layout', "vertical"); // display Layout
$show_addtocart = (bool)$params->get('show_addtocart', 1); // display Cart Button
$show_price= (bool)$params->get('show_price', 1); // display Price	

// pass the value to xmlrpc
$msg = new xmlrpcmsg("mallkit.getProductsFeatured", array(new xmlrpcval($numtodisplay, $xmlrpcInt), new xmlrpcval($categories, $xmlrpcString)));

// fetching value back from xmlrpc
$response = $client->send($msg);

// BEGIN - FEATURED PRODUCT LIST
if ((bool)$response->value())
{
	// decoding the xmlrpc response into array
	$products = php_xmlrpc_decode($response->value());

	// To avoid divide by zero error
	if(count($products))
	{
					
		// This will balance the display width per product if layout is horizontal
		$width = 100/count($products);
			
		// extracting the array returned from xmlrpc and display in module
		foreach($products as $product) 
		{
			if($display_layout == "horizontal") 
			{
				echo '<div align="center" style="float:left;width:' . $width . '%;">';
			}
			else
			{
				echo '<div align="center">';
			}
	
			echo '<a href="#"><img width="100px" src="'.$instance_path.'/images/images/'.$product['image'].'" /></a>';
			echo '<div>';
			echo '<a title="'.$product['name'].'" href="#">'.$product['name'].'</a>';
			echo '</div>';
	
			// display price of the product
			if($show_price)
			{
				echo '<div>Price: '.$product['price'].'</div>';
			}

			// display the add to cart button
			if($show_addtocart)
			{
				echo '<div><input name="addtocart" class="button" type="button" value="Add to Cart" /></div>';
			}

			echo '</div>';
		}
	}
	else
	{
		echo ' -- No Product Available -- ';
	}

	echo '<div style="clear:both;"></div>';
}
else
{

	// error alert if xmlrpc call connection unsuccessfull
	echo "Error to procedure call.";
}

// END - FEATURED PRODUCT LIST
?>